<?php
/**
 * 景点
 */
namespace App\Http\Controllers\Admin\Business;


use App\Http\Controllers\Admin\Business\Service\Combo;
use App\Http\Controllers\Admin\Business\Service\ComboActivities;
use App\Http\Controllers\Admin\Business\Service\ComboOrder;
use App\Http\Controllers\Admin\Business\Repository\ComboOrder as ComboOrderRepository;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;

class ComboOrderController extends Controller
{
    private $manager;
    private $repository;
    private $request;
    private $viewPrefix = 'admin.business.comboOrder.';
    private $routePrefix = 'admin.business.comboOrder';
    public function __construct (Request $request)
    {
        $this->manager = new ComboOrder();
        $this->repository = new ComboOrderRepository();
        $this->request = $request;
    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){


        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{

            $price=$this->manager->getListCount($this->request);
            return view($this->viewPrefix.'index',[
                'manager'   =>  $this->manager,
                'price'=>$price,
                'routePrefix'   =>  $this->routePrefix,
            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){

        return $this->page_json_render($this->manager->getPageList($this->request));
    }

    public function read($id){
        $model  = $this->manager->getComboOrder($id);
        $sysRate = [
            'rate'  =>  Setting::getConfig('rate','分销设置'),
            'top_rate'  =>  Setting::getConfig('topRate','分销设置')
        ];

        $reduce_type = ComboActivities::REDUCE;
        return view($this->viewPrefix.'read',[
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix,
            'model' =>  $model,
            'rate_type_user' =>  Combo::RATE_TYPE_USER,
            'sysRate'   =>  $sysRate,
            'reduce_type'   =>  $reduce_type
        ]);
    }
    public function send(Request $request){
        $comboOrderId=$request->combo_order_id;
        $comboOrder=\App\Http\Controllers\Admin\Business\Repository\ComboOrder::find($comboOrderId);
        if($comboOrder->is_send==1){
            return \Response::json(
                [
                    'code'=>0,
                    'message'=>'门票已经发放'
                ]
            );
        }
        $comboOrder->is_send=1;
        $comboOrder->save();
        return \Response::json([
            'code'=>1,
            'message'=>'门票发放成功'
        ]);
    }
    /**
     * @return array
     */
    private function hide(){
        $this->manager->hide($this->request->id);
        return  ['info'  =>  'success'];
    }
    private function show(){
        $this->manager->show($this->request->id);
        return  ['info'  =>  'success'];
    }
    public function exportReport(){
        $cellData = $this->manager->exportList($this->request);
        Excel::create('支付订单',function($excel) use ($cellData){
            $excel->sheet('orders', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }

}
